// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import {getMenuApi} from "@/api/layout"
import {generateMenu} from "./router/index";
import Pagination from "./components/Pagination";
import eIconPicker from 'e-icon-picker';
import "e-icon-picker/lib/symbol.js"; //基本彩色图标库
import 'e-icon-picker/lib/index.css'; // 基本样式，包含基本图标
import 'element-ui/lib/theme-chalk/index.css'
import element from './element/index'
import 'font-awesome/css/font-awesome.min.css'; //font-awesome 图标库
import 'element-ui/lib/theme-chalk/icon.css'; //element-ui 图标库
// 全局引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
Vue.use(eIconPicker, {FontAwesome: true, ElementUI: true, eIcon: true, eIconSymbol: true});
Vue.use(element)
Vue.config.productionTip = false
Vue.prototype.generateMenu = generateMenu;
Vue.component('Pagination', Pagination)

router.beforeEach((to, from, next) => {
  //to到哪儿 from从哪儿离开 next跳转 为空就是放行
   if (to.path === '/') {
   //如果跳转为登录，就放行
    next();
   } else {
   //取出localStorage判断
    let token = localStorage.getItem('token');
    if (token == null || token === '') {
      next('/');
      Message.error({ message: "你还没有登录呢，请先登录！"});
    }else{
      if(!store.state.token && from.fullPath != "/index"){
        getMenuApi().then((data)=> {
          store.state.menus = generateMenu(data.data);
        }).catch(()=>{
          Message.error({ message: "路由异常"});
        })
        store.state.token = window.localStorage.getItem("token");
        store.state.refresh = true;
        next(to.fullPath)
      }else{
        next();
      }

    }
  }});
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  store,
  data(){
    return {
      userMenu:[]
    }
  }
})
